<?php
/**
 * 订单 model 类
 *
 * @author <judasnow@gmail.com>
 */
require_once( APPPATH . 'models/base_model.php' );

class Order_model extends base_model{

	private $_table_name;
	private $_CI;
	//交易状态常量
	const WAIT_BUYER_PAY = '等待买家付款',
		WAIT_SELLER_SEND_GOODS = '买家已付款，等待卖家发货',
		WAIT_BUYER_CONFIRM_GOODS = '卖家已发货，等待买家确认',
		TRADE_FINISHED = '交易成功结束',
		TRADE_CLOSED = '交易中途关闭（已结束，未成功完成)';

	public function __construct(){
		$this->_CI =& get_instance();
		$this->_table_name = 'order';
		parent::__construct( $this->_table_name );
	}

	//根据传入的 cond 
	private function make_where_by_cond( $cond ){
		switch( $cond ){
		case 'today':
			$today =  date( 'Y-m-d 0:0:0' , strtotime( 'now' ) );
			$tomorrow =  date( 'Y-m-d 0:0:0' , strtotime( '+1 day' ) );
			return array( 'time <'=>$tomorrow , 'time >'=>$today );
			break;
		}
	}

	//根据条件获取全部的交易信息
	public function find_all( $cond = '' , $order_by = '' , $per_pager = '' , $offset = '' , $search_sql = '' ){
		 $this->load->model( 'product_class_model' , '' , TRUE );
		 $this->load->model( 'product_model' , '' , TRUE );
		 $this->load->model( 'user_model' , '' , TRUE );
		 if( !empty( $search_sql ) ){
			 $sql_limit = !empty( $per_pager ) ? "LIMIT $per_pager" : '';
			 $sql_offset = !empty( $offset ) ? " OFFSET $offset" : ''; 
			 $sql = $search_sql . " $sql_limit $sql_offset";
			 $q = $this->db->query( $sql );
			 $order_list = $q->result_array();	 
		 }else{
			 if( !is_array( $cond ) ){
				 //如果条件不是数组
				 //则调用 make_where_by_cond 函数生成既有的条件 比如 today 
				 $cond = $this->make_where_by_cond( $cond );
			 }
			 //原始的订单列表	
			 $order_list = parent::find_all( $cond  , $order_by  , $per_pager , $offset );
		 }
		 return $order_list;
	}

	/**
	 * 检测订单的有效性
	 *
	 * @param array $order_info 订单信息
	 */
	public function check_order( $order_info ){
		 //数量不能为 0
		 if( $order_info['quantity'] == 0 ){
			 throw new RuntimeException( '购买商品的数量不能为0' );
		 }
		 //判断购买发生的时刻所买的商品的信息是
		 //否还有效
		 $this->_CI->load->model( 'product_model' , '' , TRUE );
		 if( !$this->_CI->product_model->is_exists( array( 'product_id'=>$order_info['product_id'] ) ) ){
			 throw new RuntimeException( '订单中所购买的商品当前无效' ); 
		 }
		 $this->_CI->load->model( 'product_class_model' , '' , TRUE );
		 if( !$this->_CI->product_class_model->is_exists( array( 'class_id'=>$order_info['product_class_id'] ) ) ){
			 throw new RuntimeException( '订单中所购买的商品所属的服务信息当前无效' );
		 }
		 $this->_CI->load->model( 'product_at_spec_class_model' , '' , TRUE );
		 if( !$this->_CI->product_at_spec_class_model->is_exists(
			 array( 
				 'product_id'=>$order_info['product_id'] , 
				 'product_class_id'=>$order_info['product_class_id'] ) ) ){
			 throw new RuntimeException( '订单中的商品，服务没有相关联的信息' );
		 }
		 return TRUE;
	}

	/**
	 * 添加新的交易信息到数据库中
	 *
	 * @param array $new_order_info 订单信息
	 * @return int $order_sn 返回订单序号
	 */
	public function add_new( $new_order_info ){
		if( $this->check_order( $new_order_info ) ){
			//生成 order 串号 16 位
			$new_order_info['order_sn'] = date( 'Ymd' ) . substr( time() , -8 );
			parent::add_new( $new_order_info );

			return $new_order_info['order_sn'];
		}
	}

	//设置查询范围为当天
	private function set_where_in_today(){
		$today =  date( 'Y-m-d 0:0:0' , strtotime( 'now' ) );
		$tomorrow =  date( 'Y-m-d 0:0:0' , strtotime( '+1 day' ) );
		$this->db->where( 'time <' , $tomorrow )->where( 'time >' , $today );
	} 

	/**
	 * 根据商品信息获取的交易量 同时返回当日交易量以及总的交易量
	 */
	public function get_product_order_stat( $product_id = '' , $product_class_id = '' ){
		if( !is_numeric( $product_class_id ) || !is_numeric( $product_id ) ){
			 return;
		}
		$this->db->where( 'product_class_id' , $product_class_id )->where( 'product_id' , $product_id )->select( 'count(*) as `sum`' )->group_by( 'product_id' );
		//获取当日的交易量
		$this->set_where_in_today();
		$q = $this->db->get( 'order' );
		$sales_today = $q->result_array();
		if( empty( $sales_today ) ){
			 $sales_today[0]['sum'] = 0;
		}
		//获取总的交易量
		$this->db->where( 'product_class_id' , $product_class_id )->where( 'product_id' , $product_id )->select( 'count(*) as `sum`' )->group_by( 'product_id' );
		$q = $this->db->get( 'order' );
		$sales = $q->result_array();
		if( empty( $sales ) ){
			 $sales[0]['sum'] = 0;
		}
		return array( 'sales_today'=>$sales_today[0]['sum'] , 'sales'=>$sales[0]['sum'] );
	}

	//获取本日的总的交易数
	public function get_today_order(){	
                $this->set_where_in_today();
                $this->db->order_by( 'time' , 'DESC' );
		$res = $this->db->get( 'order' );
		$today_order = $res->result_array();
		$count_today_order = count( $today_order );

		$stat_product_class = '';
		if( $count_today_order > 0 ){
			//统计的商品分类信息
			$this->set_where_in_today();
			$this->db->group_by( 'product_class_id' )->select( 'count(*) as `sum`, product_class_id' );
			$p = $this->db->get( 'order' );
			$stat_product_class = $p->result_array();
		 	//获取相关服务的名称
		 	$this->load->model( 'product_class_model' , '' , TRUE );
			foreach( $stat_product_class as $index=>$item ){
		 		$stat_product_class[$index]['class_name'] = $this->product_class_model->get_class_name_by_id( $item['product_class_id'] );
			 	//计算百分比
			 	$stat_product_class[$index]['ratio'] = ( round( $stat_product_class[$index]['sum'] / $count_today_order , 4 ) ) * 100;
			}
		}
		return array( 'count_today_order'=>$count_today_order , 'stat_product_class'=>$stat_product_class , 'today_order'=>$today_order );
	}
}

